var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

var app = getApp();

var pageNo = 1;
var hadLastPage = false;
var isLoad = 0;
var isOperate = 0;

Page({

  data: {
    type: 0,
    commissionList: [],
    commission: 0,
    historyCommission: 0
  },
  yongjinSelect: function (e) {
    if (isLoad == 1) {
      return;
    }
    this.setData({
      type: e.target.dataset.type,
      commissionList: []
    })
    pageNo = 1;
    hadLastPage = false;
    this.loadList();
  },
  onShow: function () {
    this.getMemberInfo();
    pageNo = 1;
    hadLastPage = false;
    this.setData({
      commissionList: []
    });
    this.loadList();
  },
  onReachBottom: function () {
    this.loadList();
  },
  loadList: function () {
    isOperate = 0;
    isLoad = 1;
    if (hadLastPage) {
      // wx.showToast({
      //   title: '到底了！',
      // });
      isLoad = 0;
      return;
    }
    var that = this;
    var data = {
      pageNo: pageNo,
      type: that.data.type
    }
    util.request(api.CommissionList, data, 'POST').then(function (data) {
      if (data.type == 'success') {
        var commissionList = that.data.commissionList;
        for (var i = 0; i < data.data.commissionList.length; i++) {
          commissionList.push(data.data.commissionList[i]);
        }
        if (data.data.pageNo == data.data.totalPages) {
          hadLastPage = true;
        } else {
          pageNo++;
        }
        that.setData({
          commissionList: commissionList
        });
        isLoad = 0;
      } else {
        console.log(data.content);
        isLoad = 0;
      }
    });
  },
  getMemberInfo: function () {
    var that = this;
    util.request(api.MemberInfoUrl, {}, 'GET').then(function (data) {
      if (data.type === 'success') {
        that.setData({
          memberType: data.data.member.type,
          memberId: data.data.member.id,
          commission: data.data.member.commission.toFixed(2) / 1,
          historyCommission: data.data.member.historyCommission.toFixed(2) / 1,
          ch: (data.data.member.historyCommission - data.data.member.commission).toFixed(2)
        });
      } else {
        console.log(data.content);
      }
    });
  },
  tixian: function () {
    wx.navigateTo({
      url: '/pages/ucenter/tixian/tixian',
    })
  }

})